package com.qiwenshare.file.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiwenshare.file.component.InitData;
import com.qiwenshare.file.domain.Department;
import com.qiwenshare.file.domain.RoleMenu;
import com.qiwenshare.file.domain.SysMenu;
import com.qiwenshare.file.mapper.RoleMenuMapper;
import com.qiwenshare.file.mapper.SysMenuMapper;
import com.qiwenshare.file.api.SysMenuService;
import com.qiwenshare.file.util.SpringBeanUtil;
import com.qiwenshare.file.vo.department.DepartmentTreeVo;
import com.qiwenshare.file.vo.menu.MenuVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单(SysMenu)表服务实现类
 *
 * @author MJ
 * @since 2023-09-20 11:11:45
 */
@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param menuId 主键
     * @return 实例对象
     */
    @Override
    public SysMenu queryById(String menuId) {
        return this.sysMenuMapper.queryById(menuId);
    }

    /**
     * 分页查询
     *
     * @param sysMenu     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysMenu> queryByPage(SysMenu sysMenu, PageRequest pageRequest) {
        long total = this.sysMenuMapper.count(sysMenu);
        return new PageImpl<>(this.sysMenuMapper.queryAllByLimit(sysMenu, pageRequest), pageRequest, total);
    }

    @Override
    public List<MenuVo> getAllMenuTree() {
        return null;
    }

    /**
     * 新增数据
     *
     * @param sysMenu 实例对象
     * @return 实例对象
     */
    @Override
    public SysMenu insert(SysMenu sysMenu) {
        this.sysMenuMapper.insert(sysMenu);
        return sysMenu;
    }

    /**
     * 修改数据
     *
     * @param sysMenu 实例对象
     * @return 实例对象
     */
    @Override
    public SysMenu update(SysMenu sysMenu) {
        this.sysMenuMapper.update(sysMenu);
        return this.queryById(sysMenu.getMenuId());
    }

    /**
     * 通过主键删除数据
     *
     * @param menuId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String menuId) {
        return this.sysMenuMapper.deleteById(menuId) > 0;
    }

    @Override
    public List<MenuVo> getMenuTree(String userId) {
        List<SysMenu> menus = sysMenuMapper.selectList(new QueryWrapper<>());
        List<MenuVo> treeList = new ArrayList<>();

        for (SysMenu menu : menus) {
            if (StringUtils.isBlank(menu.getParentId())) {
                MenuVo treeVo = SpringBeanUtil.copyProperties(menu, MenuVo.class);
                treeVo.setPath(menu.getUrl());
                treeVo.setName(menu.getMenuName());
                treeList.add(treeVo);
            }
        }
        generateTree(menus, treeList);
        return treeList;
    }

    @Override
    public List<MenuVo> getMenuTreeByUserId(String userId) {
        List<SysMenu> menus = sysMenuMapper.selectListByUserId(userId);
        List<MenuVo> treeList = new ArrayList<>();

        for (SysMenu menu : menus) {
            if (StringUtils.isBlank(menu.getParentId())) {
                MenuVo treeVo = SpringBeanUtil.copyProperties(menu, MenuVo.class);
                treeVo.setPath(menu.getUrl());
                treeVo.setName(menu.getMenuName());
                treeList.add(treeVo);
            }
        }
        generateTree(menus, treeList);
        return treeList;
    }

    @Override
    public List<RoleMenu> getMenuByRole(Long roleId) {
        return roleMenuMapper.selectList(new QueryWrapper<>(RoleMenu.builder()
                .roleId(roleId)
                .build()));
    }

    private void generateTree(List<SysMenu> menuList, List<MenuVo> treeList) {
        for (MenuVo treeVo : treeList) {
            List<MenuVo> children = new ArrayList<>();
            for (SysMenu menu : menuList) {
                if (treeVo.getMenuId().equals(menu.getParentId())) {
                    MenuVo dVo = SpringBeanUtil.copyProperties(menu, MenuVo.class);
                    dVo.setPath(menu.getUrl());
                    dVo.setName(menu.getMenuName());
                    children.add(dVo);
                }
            }
            treeVo.setChildren(children);
            if (CollectionUtil.isNotEmpty(children)) {
                generateTree(menuList, treeVo.getChildren());
            }

        }
    }
}
